package com.dolphin.crossbrowser;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import org.xwalk.core.XWalkInitializer;
import org.xwalk.core.XWalkSettings;
import org.xwalk.core.XWalkView;

public class MainActivity extends Activity {
    public static final String TAG = "MainActivity";

    public static final String DESKTOP_USERAGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36";
    private XWalkView mXWalkView;

    private XWalkInitializer mXWalkInitializer;
    private XWalkInitListenerImpl mInitListener;

    private XWalkUIClientImpl mXWalkUIClient;
    private XWalkResourceClientImpl mXWalkResourceClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Must call initAsync() before anything that involes the embedding API, including
        // invoking setContentView() with the layout which holds the XWalkView object.
        mInitListener = new XWalkInitListenerImpl();
        mXWalkInitializer = new XWalkInitializer(mInitListener, this);
        mXWalkInitializer.initAsync();

        // Until onXWalkInitCompleted() is invoked, you should do nothing with the embedding API
        // except the following:
        // 1. Instanciate the XWalkView object
        // 2. Call XWalkPreferences.setValue()
        // 3. Call XWalkView.setUIClient()
        // 4. Call XWalkView.setResourceClient()
        setContentView(R.layout.activity_main);
        mXWalkView = (XWalkView) findViewById(R.id.activity_main);

        // set client
        mXWalkUIClient = new XWalkUIClientImpl(mXWalkView);
        mXWalkResourceClient = new XWalkResourceClientImpl(mXWalkView);
        mXWalkView.setUIClient(mXWalkUIClient);
        mXWalkView.setResourceClient(mXWalkResourceClient);
    }

    private void initSettings() {
        XWalkSettings settings = mXWalkView.getSettings();
        settings.setUseWideViewPort(true);
        //settings.setUserAgentString(DESKTOP_USERAGENT);
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (mXWalkView != null) {
            Log.d(TAG, "onActivityResult");
            mXWalkView.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        if (mXWalkView != null) {
            Log.d(TAG, "onNewIntent");
            mXWalkView.onNewIntent(intent);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        // Try to initialize again when the user completed update and returned to current
        // activity. The initAsync() will do nothing if the initialization has already been
        // completed successfully or previous update dialog is still being displayed.
        mXWalkInitializer.initAsync();
    }

    // init listener
    class XWalkInitListenerImpl implements XWalkInitializer.XWalkInitListener {
        @Override
        public void onXWalkInitStarted() {
            Log.d(TAG, "onXWalkInitStarted");
        }

        @Override
        public void onXWalkInitCancelled() {
            Log.d(TAG, "onXWalkInitCancelled");

            // The user clicked the "Cancel" button during download.
            // Perform error handling here.
        }

        @Override
        public void onXWalkInitFailed() {
            Log.d(TAG, "onXWalkInitFailed");
        }

        @Override
        public void onXWalkInitCompleted() {
            Log.d(TAG, "onXWalkInitCompleted");

            initSettings();
            mXWalkView.load("http://www.baidu.com/", null);
        }
    }
}
